package com.yanqu.road.server.timer.activity;

import com.yanqu.road.logic.config.ServerConfig;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class AutoSendActivityRewardTask extends GeneralTask {

    private Logger logger = LogManager.getLogger(this.getClass().getName());

    private final long CHECK_TIME = 60000;              //一分钟检测一次

    private final long RECORD_CHECK_TIME = 3000;        //3秒检测记录开始的冲榜活动

    private long lastCheckTime;

    private long lastRecordCheckTime;

    private boolean isRun = false;

    @Override
    public void doRun() {
        if(ServerConfig.ACTIVITY_AUTO_SEND_RANK_REWARD > 0) {
            long nowTime = System.currentTimeMillis();
            if (nowTime - lastRecordCheckTime > RECORD_CHECK_TIME) {
                NormalActivityMgr.recordRankActivity();
                lastRecordCheckTime = nowTime;
            }
            if (nowTime - lastCheckTime > CHECK_TIME && !isRun) {
                isRun = true;
                try {
                    NormalActivityMgr.checkAutoSendActivityReward();
                    lastCheckTime = nowTime;
                    logger.info("自动发放未领取活动奖励检测");
                }catch (Exception e){
                    logger.error("auto send activity reward error:", e);
                }finally {
                    isRun = false;
                }
            }
        }
    }
}
